<?php

require_once 'classes/LightPressPlugin.php';

class MetaDescription extends LightPressPlugin {
    
    var $constructor_args = array(
        'blog_description'  =>'custom blog description instead of the one from the WP options',
        'excerpt_length'    => "maximum length in words for the excerpt, set to '0' to disable",
        'ignore_excerpt'    => 'ignore the stored excerpt, and always use the post content to generate a new one',
        'custom_key'        => 'get the description from this custom post key instead of using/generating the excerpt');
    var $default_context = 24; // LP_CONTEXT_POST | LP_CONTEXT_STATIC
    var $description = 'Add a meta description header for posts and pages';
    var $textareas = array('blog_description');
    
    var $active = true;
    var $hooks = array('parse_post');
    
    var $blog_description;
    var $excerpt_length = 25;
    var $ignore_excerpt = false;
    var $custom_key = '';
    
    function MetaDescription(&$frontend, $args, $dummy_run=false) {
        
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        
        if (!$dummy_run) {
            if (!in_array($frontend->context['type'], array('post', 'static'))) {
                $this->active = false;
                return;
            }
            if (empty($this->excerpt_length))
                $this->excerpt_length = 0;
            else
                $this->excerpt_length = (int)$this->excerpt_length;
        }
    }
    
    function run($hook, &$post) {
        
        $frontend =& $this->_frontend;
        
        $tpl =& $frontend->tpl;
        $db =& $frontend->db;
        
        if ($this->ignore_excerpt or empty($post['post_excerpt']))
            $excerpt = $post['post_content'];
        else
            $excerpt = $post['post_excerpt'];
        
        if (!is_null($this->custom_key)) {
            $q = "select meta_value from {$this->_frontend->tables['postmeta']} where post_id = {$post['post_id']} and meta_key = '" . addslashes($this->custom_key) . "'";
            $count = $db->query($q);
            if ($count)
                $excerpt = stripslashes($db->get('meta_value'));
        }
        
        $excerpt =& $this->_frontend->getExcerpt($excerpt, $this->excerpt_length);
        
        $tpl->setVar('PLUGIN_METADESCRIPTION', sprintf('<meta name="description" content="%s" />', htmlspecialchars($excerpt, ENT_COMPAT, $this->_frontend->options['charset'])));
        
    }
    
}

?>